package com.jc.subject.domin.service.impl;

import com.alibaba.fastjson.JSON;
import com.jc.subject.common.enums.CategoryTypeEnum;
import com.jc.subject.common.enums.IsDeletedFlagEnum;
import com.jc.subject.domin.convert.SubjectCategoryConverter;
import com.jc.subject.domin.entity.SubjectCategoryBO;
import com.jc.subject.domin.entity.SubjectLabelBO;
import com.jc.subject.domin.service.SubjectCategoryDomainService;
import com.jc.subject.infra.basic.entity.SubjectCategory;
import com.jc.subject.infra.basic.entity.SubjectLabel;
import com.jc.subject.infra.basic.entity.SubjectMapping;
import com.jc.subject.infra.basic.service.SubjectCategoryService;
import com.jc.subject.infra.basic.service.SubjectLabelService;
import com.jc.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: 题目分类领域服务
 * @Author: ASL_ly
 * @Package: com.jc.subject.domin.service.impl
 * @Project: jc-club
 * @Date: 2024/10/21  19:29
 */
@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectLabelService subjectLabelService;

    /**
     * 新增分类
     * @param subjectCategoryBO
     */
    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.add.bo:{}", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        subjectCategoryService.insert(subjectCategory);
    }

    /**
     * 查询大类分类
     * @return
     */
    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter
                .INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryConverter
                .INSTANCE.convertCategoryToBOList(subjectCategoryList);
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.queryPrimaryCategory.boList:{}", JSON.toJSONString(subjectCategoryBOList));
        }
        subjectCategoryBOList.forEach(bo ->{
            Integer subjectCount = subjectCategoryService.querySubjectCount(bo.getId());
            bo.setCount(subjectCount);
        });
        return subjectCategoryBOList;
    }

    /**
     * 修改分类
     * @param subjectCategoryBO
     */
    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.update.bo:{}",JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter
                .INSTANCE.convertBoToCategory(subjectCategoryBO);
       Integer count = subjectCategoryService.update(subjectCategory);
       return count > 0 ;
    }

    /**
     * 删除分类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.delete.bo:{}",JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter
                .INSTANCE.convertBoToCategory(subjectCategoryBO);
        //逻辑删除
        subjectCategory.setIsDeleted(IsDeletedFlagEnum.DELETED.code);
        Integer count = subjectCategoryService.update(subjectCategory);
        return count > 0;
    }

    /**
     * 查询分类及标签
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public List<SubjectCategoryBO> queryCategoryAndLabel(SubjectCategoryBO subjectCategoryBO) {
        //查询大类下所有分类
        SubjectCategory subjectCategory = SubjectCategory.builder()
                .parentId(subjectCategoryBO.getId())
                .isDeleted(IsDeletedFlagEnum.UN_DELETED.code)
                .build();
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.queryCategoryAndLabel.subjectCategoryList:{}"
                    ,JSON.toJSONString(subjectCategoryList));
        }
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryConverter
                .INSTANCE.convertCategoryToBOList(subjectCategoryList);
        //一次获取标签信息
        subjectCategoryBOList.forEach(categoryBO ->{
            SubjectMapping subjectMapping = SubjectMapping.builder()
                    .categoryId(categoryBO.getId())
                    .build();
            List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelIdList(subjectMapping);
            if(subjectMappingList.isEmpty()){
                return;
            }
            List<Long> labelIdList = subjectMappingList.stream()
                    .map(SubjectMapping::getLabelId).collect(Collectors.toList());
            List<SubjectLabel> subjectLabelList = subjectLabelService.batchQuerryById(labelIdList);
            List<SubjectLabelBO> subjectLabelBOList = new LinkedList<>();
            subjectLabelList.forEach(label -> {
               SubjectLabelBO subjectLabelBO = new SubjectLabelBO();
               subjectLabelBO.setId(label.getId());
               subjectLabelBO.setCategoryId(label.getCategoryId());
               subjectLabelBO.setLabelName(label.getLabelName());
               subjectLabelBO.setSortNum(label.getSortNum());
               subjectLabelBOList.add(subjectLabelBO);
            });
            categoryBO.setLabelBOList(subjectLabelBOList);
        });
        return subjectCategoryBOList;
    }
}
